package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.PlaceService;

/**
 * Servlet implementation class RegisterPlaceServlet
 */
@WebServlet("/RegisterPlaceServlet")
public class RegisterPlaceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterPlaceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    
    /**
     * Invokes the action "registerplace.jsp"
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		RequestDispatcher rd = request.getRequestDispatcher("registerplace.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	/**
	 * Method invoked when trying to register a place
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
 
		HttpSession session = request.getSession(true);
		
		
		/**
		 * Parameters obtained form
		 */
		String pn = request.getParameter("placename");
		String pd = request.getParameter("placedescription");
		String c = request.getParameter("capacityplace");
		String pz = request.getParameter("placezones");
		String pt = request.getParameter("placetypes");
		
		
		
		int cN = Integer.parseInt(c);
	
		PlaceService service = new PlaceService();
		Place place = new Place();
		
	    place.setNamePlace(pn);
	    place.setPlaceDescription(pd);
	    place.setCapacityPlace(cN);
	    place.setZonePlace(pz);
	
		
	    
	    try {
	    	/**
	    	 * Enter a place
	    	 */
	    	service.insertPlace(place, pt);
	    	
	    	/**
			 * Invokes the action "adminvisitor.jsp"
			 */
	    	//request.getRequestDispatcher("adminvisitor.jsp").forward(request, response);
	    	response.sendRedirect("ShowPlacesAdminServlet");
	    	
		} catch (ServiceException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} 
		
	}

}
